//
//  ContentScrollView.m
//  SohoNaru
//
//  Created by ladaka31 ladaka on 13. 5. 31..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.
//

#import "ContentScrollView.h"
#import "ContentView.h"

@implementation ContentScrollView

@synthesize contentView;

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:CGRectMake(0,40, 320, 265)];
    
    if (self) {
        
        self.bounces = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        // Initialization code
        contentView = [[ContentView alloc] initWithFrame:CGRectMake(8, 95, 304, 430)];
        [self addSubview:contentView];
       
        CGSize imageSize = contentView.frame.size;
        CGSize maxSize = self.frame.size;
        CGFloat widthRadio = maxSize.width / imageSize.width;
        CGFloat heightRatio = maxSize.height / imageSize.height;
        CGFloat initalZoom = (widthRadio > heightRatio) ? widthRadio : heightRatio;
        
//        self.delegate = self;
        self.minimumZoomScale = initalZoom;
        self.maximumZoomScale = 2.0f;
        self.contentSize = imageSize;
        self.zoomScale = initalZoom;
        
        self.contentSize = contentView.frame.size;
    }

    return self;

}

- (void) awakeFromNib {
    [super awakeFromNib];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureCaptured:)];
    [self addGestureRecognizer:tapGesture];
    
    [tapGesture setNumberOfTapsRequired:1];
    [tapGesture setNumberOfTouchesRequired:1];
    [self addGestureRecognizer:tapGesture];
}


- (void)tapGestureCaptured:(UITapGestureRecognizer *)gesture
{
//    CGPoint touchPoint=[gesture locationInView:self];
    
//    NSLog(@"point - x:%f, y:%f", touchPoint.x, touchPoint.y);
    
    if ([self.delegate respondsToSelector:@selector(didTapGesture:)]) {
        [self.delegate didTapGesture:gesture];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    [self.nextResponder touchesBegan:touches withEvent:event];
    NSLog(@"touchesBegan1");
    [super touchesBegan:touches withEvent:event];
    
    UITouch *touch = [touches anyObject];
    if ([touch tapCount] == 2) {
        if (self.zoomScale == self.minimumZoomScale)
            [self setZoomScale:self.maximumZoomScale animated:YES];
        else if (self.zoomScale == self.maximumZoomScale)
            [self setZoomScale:self.minimumZoomScale animated:YES];
        else
            [self setZoomScale:self.minimumZoomScale animated:YES];
    }
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return contentView;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGSize boundsSize = self.bounds.size;
    CGRect frameToCenter = contentView.frame;
    
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width)/2;
    else
        frameToCenter.origin.x = 0;
    
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height)/2;
    else
        frameToCenter.origin.y = 0;
    
    contentView.frame = frameToCenter;
}


@end
